package com.erlin.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erlin.common.exception.ElException;
import com.erlin.common.utils.JwtTokenUtils;
import com.erlin.common.utils.MessageUtils;
import com.erlin.platform.constant.user.UserStatus;
import com.erlin.platform.entity.ElRole;
import com.erlin.platform.entity.ElUser;
import com.erlin.platform.entity.UserRoleMid;
import com.erlin.platform.service.IElRoleService;
import com.erlin.platform.service.IUserRoleMidService;
import com.erlin.store.constant.StoreConstant;
import com.erlin.store.constant.StoreMemberConstant;
import com.erlin.store.constant.StoreMemberTypeEnum;
import com.erlin.store.controller.param.StoreMemberAddParam;
import com.erlin.store.entity.Store;
import com.erlin.store.entity.StoreMember;
import com.erlin.store.mapper.StoreMemberMapper;
import com.erlin.store.service.IStoreMemberService;
import com.erlin.store.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author alin
 * @since 2021-06-01
 */
@Service
public class StoreMemberServiceImpl extends ServiceImpl<StoreMemberMapper, StoreMember> implements IStoreMemberService {
    @Autowired
    private IStoreService storeService;
    @Autowired
    private IElRoleService elRoleService;
    @Autowired
    private IUserRoleMidService userRoleMidService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMember(StoreMemberAddParam param) {
        ElUser currentUser = JwtTokenUtils.getCurrentUser();
        if (Objects.equals(UserStatus.DISABLE, currentUser.getStatus())) {
            throw new ElException(MessageUtils.getMessage("user.status.disable"));
        }
        Store store = checkParam(param);
        //添加member信息
        StoreMember target = new StoreMember();
        target.setStoreId(store.getId());
        target.setStoreMid(store.getMid());
        target.setMemberStatus(StoreMemberConstant.STATUS_ENABLE);
        target.setMemberType(param.getMemberType());
        target.setUserId(currentUser.getId());
        save(target);
        //判断此角色是否有对应的角色，如果没有，需要添加相关角色
        String userClas = StoreMemberTypeEnum.getEnumStr(param.getMemberType());
        long roleId = Optional.ofNullable(elRoleService.getOne(Wrappers.<ElRole>lambdaQuery().eq(ElRole::getUserClas, userClas))).orElseThrow(() -> new ElException(MessageUtils.getMessage("sys.role.data.error"))).getId();
        UserRoleMid one = userRoleMidService.getOne(Wrappers.<UserRoleMid>lambdaQuery()
                .eq(UserRoleMid::getRoleId, roleId)
                .eq(UserRoleMid::getUserId, currentUser.getId())
        );
        if (Objects.isNull(one)) {
            userRoleMidService.save(new UserRoleMid(currentUser.getId(), roleId));
        }
        return true;
    }

    private Store checkParam(StoreMemberAddParam param) {
        Store store = Optional.ofNullable(storeService.getOne(Wrappers.<Store>lambdaQuery().eq(Store::getMid, param.getStoreMid())))
                .orElseThrow(() -> new ElException(MessageUtils.getMessage("store.not.exist")));
        if (Objects.equals(StoreConstant.STORE_STATUS_CLOSURE, store.getStoreStatus()) || Objects.equals(StoreConstant.AUTH_STATUS_REJECT, store.getAuthStatus())) {
            throw new ElException(MessageUtils.getMessage("store.auth.status.error"));
        }
        return store;
    }
}
